﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace gChartSharp
{
    /// <summary>
    /// http://code.google.com/apis/chart/#linear_gradient
    /// </summary>
    public class ChartLinearGradientFill : IFill
    {

        /// <summary>
        /// The name of the API
        /// </summary>
        public string Name { get { return "chf"; } }


        /// <summary>
        /// Always return what type of fill we are in case we forget
        /// </summary>
        public FillType FillType { get { return FillType.lg; } }


        /// <summary>
        /// bg for background fill or c for chart area fill
        /// </summary>
        public LinearGradientFillType LinearGradientType { get; set; }


        /// <summary>
        /// Specifies the angle of the gradient between 0 (horizontal) and 90 (vertical)
        /// </summary>
        public double GradientAngle
        {
            get { return mGradientAngle; }
            set
            {
                if (value < 0 || value > 90)
                    throw new Exception("GradientAngle value must be between 0 and 90.");

                mGradientAngle = value;
            }
        }
        private double mGradientAngle;


        /// <summary>
        /// The first color of the gradient fill
        /// </summary>
        public Color Color1 { get; set; }


        /// <summary>
        /// The second color of the gradient fill
        /// </summary>
        public Color Color2 { get; set; }


        /// <summary>
        /// Specify at what point the color is pure where: 0 specifies the right-most chart position and 1 the left-most.
        /// </summary>
        public double Offset1
        {
            get { return mOffset1; }
            set
            {
                if (value < 0 || value > 1)
                    throw new Exception("Offset value must be between 0 and 1.");

                mOffset1 = value;
            }
        }
        private double mOffset1;


        /// <summary>
        /// Specify at what point the color is pure where: 0 specifies the right-most chart position and 1 the left-most.
        /// </summary>
        public double Offset2
        {
            get { return mOffset2; }
            set
            {
                if (value < 0 || value > 1)
                    throw new Exception("Offset value must be between 0 and 1.");

                mOffset2 = value;
            }
        }
        private double mOffset2;


        /// <summary>
        /// Instantiates all properties
        /// </summary>
        /// <param name="linearGradientType"></param>
        /// <param name="gradientAngle"></param>
        /// <param name="color"></param>
        /// <param name="offset"></param>
        public ChartLinearGradientFill(LinearGradientFillType linearGradientType, double gradientAngle, Color color1, double offset1, Color color2, double offset2)
        {
            this.LinearGradientType = linearGradientType;
            this.GradientAngle = gradientAngle;
            this.Color1 = color1;
            this.Offset1 = offset1;
            this.Color2 = color2;
            this.Offset2 = offset2;
        }


        /// <summary>
        /// Render the API text
        /// </summary>
        /// <returns></returns>
        public string Render()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(String.Format("&{0}={1},{2},{3},{4},{5},{6}", this.Name, this.LinearGradientType.ToString(),
                this.FillType.ToString(), ChartHelper.ConvertColorToHex(this.Color1), this.Offset1.ToString(),
                ChartHelper.ConvertColorToHex(this.Color2), this.Offset2.ToString()));
            //sb.Append(this.LinearGradientType.ToString());
            //sb.Append(",");
            //sb.Append(this.FillType.ToString());
            //sb.Append(",");
            //sb.Append(this.GradientAngle.ToString());
            //sb.Append(",");
            //sb.Append(ChartHelper.ConvertColorToHex(this.Color1));
            //sb.Append(",");
            //sb.Append(this.Offset1.ToString());
            //sb.Append(",");
            //sb.Append(ChartHelper.ConvertColorToHex(this.Color2));
            //sb.Append(",");
            //sb.Append(this.Offset2.ToString());

            return sb.ToString();
        }
    }


    public enum LinearGradientFillType
    {
        /// <summary>
        /// background fill
        /// </summary>
        bg,

        /// <summary>
        /// chart area fill
        /// </summary>
        c
    }
}
